import {data} from "./main.js";
import * as d3 from 'https://unpkg.com/d3@6?module';
export function wind(alpha){
    data.nodes.forEach(node => {
        // 随机生成速度值
        let scale = Math.random() * 10;
        const vx = (Math.random() - 0.5) * scale; // 水平方向速度
        const vy = (Math.random() - 0.5) * scale; // 垂直方向速度
        node.vx += vx / node.mass;
        node.vy += vy / node.mass;
    });
}
export const collide=d3.forceCollide().radius(d => d.size + data.nodeStrokeWidth).strength(0.15)
export function board(alpha){
    data.nodes.forEach(node => {
        const r = node.size + data.nodeStrokeWidth;
        const margin = 100;
        const leftBoundary = -data.width / 2 + margin;
        const rightBoundary = data.width / 2 - margin;
        const topBoundary = -data.height / 2 + margin;
        const bottomBoundary = data.height / 2 - margin;
        const scale = 20;
        if (node.x - r < leftBoundary) {
            node.vx += scale * alpha / node.mass; // 向右的衰减力
            // console.log(node.x - r, alpha)
        } else if (node.x + r > rightBoundary) {
            node.vx -= scale * alpha / node.mass; // 向左的衰减力
        }
        if (node.y - r < topBoundary) {
            node.vy += scale * alpha / node.mass; // 向下的衰减力
        } else if (node.y + r > bottomBoundary) {
            node.vy -= scale * alpha / node.mass; // 向上的衰减力
        }
    });
}
export const hang=d3.forceY().y(d => (-data.height / 2 + data.height * (d.levelIndex + 1) / data.levelOrder.length)).strength(d =>  (!d.levelIndex ? 0.001 : 0) * Math.sqrt(d.mass))
export const link=d3.forceLink(data.links).id(({index: i}) => data.nodes[i].id)
    .distance(link => link.length || 300)
    // .strength(link=>link.strength || 0.001)
    .strength(link => 1 / Math.min(link.source.mass, link.target.mass))
export const G=d3.forceY().y(d=>data.height/2).strength(d=>d.root?0:d.mass*0.0098)